﻿using System;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace WebGui.Admin {

    /// <summary>
    /// The code-behind class for the Admin.aspx site
    /// </summary>
    public partial class Admin : Page {

        /// <summary>
        /// When the page loads
        /// </summary>
        /// <param name="sender">The sender of the event</param>
        /// <param name="e">The event arguments</param>
        protected void Page_Load(object sender, EventArgs e) {
            if(!IsPostBack)
                UpdateUserList();
        }

        /// <summary>
        /// The create user click
        /// </summary>
        /// <param name="sender">The sender of the event</param>
        /// <param name="e">The event arguments</param>
        protected void BtnCreateUser_Click(object sender, EventArgs e) {
            MembershipCreateStatus status;
            string selectedRole = DrRoleList.SelectedItem.Text.ToString();
            string desiredUserName = txtUserName.Text.ToString();
            string generatedPassword = Membership.GeneratePassword(8, 0);
            MembershipUser NyBruger = Membership.CreateUser(desiredUserName, generatedPassword, "", "Q", "PW", true, out status);
            if(NyBruger == null) {
                lblCreateUserError.Text = LavUserFejl(status);
            } else {
                lblCreateUserInfo.Text = "" + desiredUserName + " er oprettet, koden er " + generatedPassword;
                Roles.AddUserToRole(desiredUserName, selectedRole);
                UpdateUserList();
            }
        }

        /// <summary>
        /// At row deleting
        /// </summary>
        /// <param name="sender">The sender of the event</param>
        /// <param name="e">The event arguments</param>
        protected void GvUsers_RowDeleting(object sender, GridViewDeleteEventArgs e) {
            Membership.DeleteUser(GvUsers.Rows[e.RowIndex].Cells[0].Text, true);
            UpdateUserList();
        }

        /// <summary>
        /// At row selecting
        /// </summary>
        /// <param name="sender">The sender of the event</param>
        /// <param name="e">The event arguments</param>
        protected void GvUsers_RowSelecting(object sender, EventArgs e) {
            lblNewPass.Text = "Nyt kodeord for" + GvUsers.SelectedRow.Cells[0].Text + " er " + Membership.GetUser(GvUsers.SelectedRow.Cells[0].Text).ResetPassword();
        }

        /// <summary>
        /// Update the userlist
        /// </summary>
        public void UpdateUserList() {
            GvUsers.DataSource = Membership.GetAllUsers();
            GvUsers.DataBind();
        }

        /// <summary>
        /// Return the membership errors
        /// </summary>
        /// <param name="status">The create status</param>
        /// <returns>The error message</returns>
        public string LavUserFejl(MembershipCreateStatus status) {
            switch(status) {
                case MembershipCreateStatus.DuplicateUserName:
                    return "Mailen er allerede brugt";
                case MembershipCreateStatus.InvalidUserName:
                    return "Mailen er ikke gyldig";
                case MembershipCreateStatus.ProviderError:
                    return "Et problem opstod med serveren, hvis problemet forstsætter kontakt support";
                case MembershipCreateStatus.UserRejected:
                    return "Brugeren blev afvist af serveren, hvis problemet fortsætter kontakt support";
                default:
                    return "En ukendt fejl opstod, hvis problemet fortsætter kontakt support";
            }
        }
    }
}